<?php

namespace App\Http\Controllers\Admin\SystemManage;

use App\Http\Requests\UserCreateRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Services\AdminService\Models\AdminUserModel as User;
use App\Services\AdminService\Models\AdminRoleModel as Role;
use App\Response\AdminRep;
use App\Response\TableRep;
use App\Services\AdminService\Services\RoleService;
use App\Services\AdminService\Services\UserService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\AdminService\Models\AdminUserModel;

class UserController extends Controller
{
    use AdminRep, TableRep;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $roles = RoleService::getAll();
        return view('admin.system_manage.user.index', compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.system_manage.user.create');
    }

    /**
     * 获取数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $res = UserService::getData(new AdminUserModel(), $request->all());
        return $this->table($res);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserCreateRequest $request)
    {
        $data = $request->all();
        $data['uuid'] = \Faker\Provider\Uuid::uuid();
        $data['password'] = bcrypt($data['password']);
        if (UserService::create($data)) {
            return $this->responseSucc();
        } else {
            return $this->responseFail('系统错误');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = UserService::getByIdOrFail($id);
        return view('admin.system_manage.user.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserUpdateRequest $request, $id)
    {
        $user = User::findOrFail($id);
        $data = $request->except('password');
        if ($request->get('password')) {
            $data['password'] = bcrypt($request->get('password'));
        }
        if ($user->update($data)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return response()->json(['code' => 1, 'msg' => '请选择删除项']);
        }
        if (User::destroy($ids)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }
        return response()->json(['code' => 1, 'msg' => '删除失败']);
    }

    /**
     * 分配角色
     */
    public function role(Request $request, $id)
    {
        $user = UserService::getByIdOrFail($id);
        $roles = Role::get();
        $hasRoles = $user->roles();
        foreach ($roles as $role) {
            $role->own = $user->hasRole($role) ? true : false;
        }
        return view('admin.system_manage.user.role', compact('roles', 'user'));
    }

    /**
     * 更新分配角色
     */
    public function assignRole(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $roles = $request->get('roles', []);
        if ($user->syncRoles($roles)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * 分配权限
     */
    public function permission(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $permissions = $this->tree();
        foreach ($permissions as $key1 => $item1) {
            $permissions[$key1]['own'] = $user->hasDirectPermission($item1['id']) ? 'checked' : false;
            if (isset($item1['_child'])) {
                foreach ($item1['_child'] as $key2 => $item2) {
                    $permissions[$key1]['_child'][$key2]['own'] = $user->hasDirectPermission($item2['id']) ? 'checked' : false;
                    if (isset($item2['_child'])) {
                        foreach ($item2['_child'] as $key3 => $item3) {
                            $permissions[$key1]['_child'][$key2]['_child'][$key3]['own'] = $user->hasDirectPermission($item3['id']) ? 'checked' : false;
                            if (isset($item3['_child'])) {
                                foreach ($item3['_child'] as $key4 => $item4) {
                                    $permissions[$key1]['_child'][$key2]['_child'][$key3]['_child'][$key4]['own'] = $user->hasDirectPermission($item4['id']) ? 'checked' : false;
                                }
                            }
                        }
                    }
                }
            }
        }
        return view('admin.system_manage.user.permission', compact('user', 'permissions'));
    }

    /**
     * 存储权限
     */
    public function assignPermission(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $permissions = $request->get('permissions');
        if (empty($permissions)) {
            $user->permissions()->detach();
            return $this->responseSucc();
        }
        $user->syncPermissions($permissions);
        return $this->responseSucc();
    }

}
